package com.refusea.train.domain.helper

import com.refusea.train.config.mvc.WebSession
import com.refusea.train.domain.cache.UserCache
import com.refusea.train.domain.entity.User
import com.refusea.train.mapper.OrderMapper
import com.refusea.train.mapper.UserMapper
import org.springframework.stereotype.Component

/**
 * @author Zhang Yan 2024/4/4
 **/
@Component
class StudentHelper(
    private val userMapper: UserMapper,
    private val orderMapper: OrderMapper,
    private val userCache: UserCache,
) {

    fun list(): List<User> {
        return userMapper.students(WebSession.user().schoolId)
    }

    fun ungrouped(courseId: Int): List<User> {
        // 找出已购买指定课程，还未编入班级的学生
        return orderMapper.ungroupedStudents(WebSession.user().schoolId, courseId)
            .mapNotNull { userCache.get(it) }
    }

    fun children(): List<User> {
        val user = WebSession.user()
        return userMapper.children(user.schoolId, user.id)
    }
}
